//
//  ST_AICenterFaceStatusCell.swift
//  btvideo
//
//  Created by mac on 2020/6/17.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit




class ST_AICenterFaceStatusCell: UICollectionViewCell {

    static let height = 570.cgFloat
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(face_status_background_view)
        face_status_background_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(94)
        }
        
        
        face_status_background_view.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        
        
        face_status_background_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(avatar_image_view)
            make.left.equalTo(avatar_image_view.snp.right).offset(10)
            make.height.equalTo(25)
        }
        
        face_status_background_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.left.equalTo(titleLabel)
            make.height.equalTo(17)
        }
        
        face_status_background_view.addSubview(click_label)
        click_label.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-35)
            make.height.equalTo(32)
            make.width.equalTo(100)
        }
        
        contentView.addSubview(member_label)
        
        member_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(face_status_background_view.snp.bottom).offset(20)
            make.height.equalTo(21)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(refresh_face_status), name: Notification.Name.face_status, object: nil)
        
        var lastVerticalView:ST_AICenterFaceStatusCellChildBox?
        
        for (idx,item) in ST_AICenterFaceStatusCellChildBoxModel.setup_data().enumerated(){
            
            let rowIndex = idx % 2
            
            let itemView = ST_AICenterFaceStatusCellChildBox()
            
//            itemView.backgroundColor = .random
            
            itemView.model = item
            contentView.addSubview(itemView)
            
            if rowIndex == 0{
                itemView.snp.makeConstraints { (make) in
                    make.size.equalTo(ST_AICenterFaceStatusCellChildBox.size)
                    make.right.equalTo(contentView.snp.centerX).offset(-7)
                    
                    if let last = lastVerticalView{
                        make.top.equalTo(last.snp.bottom).offset(15)
                    }else{
                        make.top.equalTo(member_label.snp.bottom).offset(15)
                    }
                }
            }else{
                
                itemView.snp.makeConstraints { (make) in
                    make.size.equalTo(ST_AICenterFaceStatusCellChildBox.size)
                    make.left.equalTo(contentView.snp.centerX).offset(8)
                    if let last = lastVerticalView{
                        make.top.equalTo(last)
                    }else{
                        make.top.equalTo(member_label.snp.bottom).offset(15)
                    }
                }
            }
            if rowIndex == 0{
                lastVerticalView = itemView
            }
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK: - Properties
    fileprivate lazy var face_status_background_view:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.set_back_image_for_all_states(ST_ImageUtils.imageName("个人中心_img_头像_已认证")!)
        control.setBackgroundImage(ST_ImageUtils.imageName("个人中心_img_头像_未认证"), for: UIControl.State.normal)
        control.add_touch_up(insideTarget: self, action: #selector(detect_handle))
        return control
    }()
    

    fileprivate lazy var avatar_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          control.cornerRadius = 22
        control.borderWidth = 1
        control.borderColor = UIColor.white
          return control
      }()

    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var click_label:UILabel = {
        let control = UILabel.init()
        control.cornerRadius = 16
        control.textAlignment = .center
        control.backgroundColor = .white
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.textColor = UIColor.init(hex: 0x42CEDA)
        return control
    }()
    
    fileprivate lazy var member_label:UILabel = {
        
        let control = UILabel.init()
        
        let attrString = NSMutableAttributedString(string: "认证福利")
        
        control.numberOfLines = 0
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        control.attributedText = attrString
        return control
    }()
    
    
}

extension ST_AICenterFaceStatusCell{
    
    @objc func detect_handle()  {
        
        
        
        
        
        
        if FACE_AVATAE_STATE == 2{
          
            let failure = ST_MatchFaceFailureController()
            
            failure.overCurrentContext = true
            
            GlobalPresent(toController: failure)
            
            return
            
        }
        
        if FACE_AVATAE_STATE == 1{
            DetectFaceHandle()
        }
        
    }
    
}

extension ST_AICenterFaceStatusCell{
    
    @objc func refresh_face_status(){
        
        guard let userInfo = ST_BTUserInfoManager.shared.current_account_info?.userinfo else { return }
                
        avatar_image_view.pin_setImage(from: userInfo.avatar.compress_small_image(width: 120).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        let nickName = NSMutableAttributedString(string: userInfo.nickname)

        nickName.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)], range: NSRange(location: 0, length: nickName.length))
        
        
        
        let status = FACE_AVATAE_STATE
        
        switch status {
        case 3:
            
            face_status_background_view.isSelected = true
            titleLabel.text = "真人头像已认证"
            titleLabel.textColor = .white
            content_label.text = "已获得优先曝光与推荐"
            content_label.textColor = .white
            click_label.isHidden = true
            break
        case 4:
            face_status_background_view.isSelected = false
            titleLabel.text = "真人识别已认证"
            titleLabel.textColor = UIColor.init(hex: 0x333333)
            content_label.text = "立刻获得优先曝光与推荐"
            content_label.textColor = UIColor.init(hex: 0x333333)
            click_label.isHidden = false
            click_label.text = "头像审核中"
            break
        default:
            
            face_status_background_view.isSelected = false
            titleLabel.text = "真人头像未认证"
            titleLabel.textColor = UIColor.init(hex: 0x333333)
            content_label.text = "立刻获得优先曝光与推荐"
            content_label.textColor = UIColor.init(hex: 0x333333)
            click_label.isHidden = false
            click_label.text = "开始认证"
            break
        }
        
        
        
    }
    
}

//MARK: - Child
class ST_AICenterFaceStatusCellChildBox: UIView {

    static let size = CGSize.init(width: (ScreenWidth - 45)/2.0, height: 95)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.cornerRadius = 10
        self.borderColor = UIColor.init(hex: 0xF5F8FA)
        self.borderWidth = 1
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(35)
            make.top.left.equalToSuperview().offset(10)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.left.equalToSuperview().offset(55)
            make.top.equalToSuperview().offset(10)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(55)
            make.top.equalToSuperview().offset(33)
            make.right.equalToSuperview().offset(-10)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 2
        return control
    }()
    
    

    fileprivate lazy var cover_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    
    var model:ST_AICenterFaceStatusCellChildBoxModel?{
        didSet{
            
            guard let m = model else { return  }
            
            cover_image_view.image = UIImage.init(named: m.imageName)
            
            titleLabel.text = m.title
            
            content_label.attributedText = m.content_attr
        }
    }
}

struct ST_AICenterFaceStatusCellChildBoxModel {
  
    
    var title:String
    var imageName:String
    var content_attr:NSAttributedString
    
    init(titleStr:String,imageNameStr:String,content:String) {
        
        title = titleStr
        
        imageName = imageNameStr
        
        let attrString = NSMutableAttributedString(string: content)
       
        let p = NSMutableParagraphStyle()
        
        p.lineSpacing = 3
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.regular),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1),.paragraphStyle:p]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
     
        content_attr = attrString
    }
    
    
    
    static func setup_data()->[ST_AICenterFaceStatusCellChildBoxModel]{
        
        let titleArray = ["真人头像标识","优先推荐曝光","聊天特别提醒","发送图片视频"]
        
        let contentArray = ["获得认证标识，让你在人群中脱颖而出","卡片、特质、日常全部优先推荐","聊天中特别告知，让你的聊天对象更放心","聊天可以发送图片和视频了"]
        
        var array = [ST_AICenterFaceStatusCellChildBoxModel]()
        
        for (i,title) in titleArray.enumerated(){
            
            let content = contentArray[i]
            
            let m = ST_AICenterFaceStatusCellChildBoxModel.init(titleStr: title, imageNameStr: "认证特权_0\(i+1)", content: content)
            
            array.append(m)
            
        }
        
        return array
        
    }
    
    
    
    static func setup_member_data()->[ST_AICenterFaceStatusCellChildBoxModel]{
        
        let titleArray = ["更多最佳精选","搜索用户","偏好设置","隐身模式"]
        
        let contentArray = ["突破人数上限，，每日推送更多最佳精选","输入昵称或特质，精准找到你要找的人","多种组合条件筛选，只看你最满意的人","在所有版块隐身，不会被别人看到！"]
        
        var array = [ST_AICenterFaceStatusCellChildBoxModel]()
        
        for (i,title) in titleArray.enumerated(){
            
            let content = contentArray[i]
            
            let m = ST_AICenterFaceStatusCellChildBoxModel.init(titleStr: title, imageNameStr: "特权_功能_icon_\(i)", content: content)
            
            array.append(m)
            
        }
        
        return array
        
    }
    
    
    static func setup_knighthood_data()->[ST_AICenterFaceStatusCellChildBoxModel]{
        
        let titleArray = ["贵族勋章","贵族礼物","贵族红色昵称","贵族聊天气泡"]
        
        let contentArray = ["专属身份勋章","贵族专属礼物","贵族专属昵称","贵族专属聊天气泡"]
        
        var array = [ST_AICenterFaceStatusCellChildBoxModel]()
        
        for (i,title) in titleArray.enumerated(){
            
            let content = contentArray[i]
            
            let m = ST_AICenterFaceStatusCellChildBoxModel.init(titleStr: title, imageNameStr: "贵族特权_0\(i+1)", content: content)
            
            array.append(m)
            
        }
        
        return array
        
    }
}

